package com.ken.fx;

import com.alibaba.fastjson.JSON;
import com.ken.ppt.ApiAuthAlgorithm;
import com.ken.ppt.ApiClient;
import com.ken.ppt.CreateResponse;
import com.ken.ppt.ProgressResponse;

import java.io.IOException;

public class createPPT {
    // 输入个人appId
    private static final String appId = "b3491fbb";
    private static final String secret = "ZjBmNWYzOGM3MzYwNTgxYjFiNjAwNTdl";
    private static final long timestamp = System.currentTimeMillis()/1000;
    private static final String ts = String.valueOf(timestamp);
    // 获得鉴权信息
    private static final ApiAuthAlgorithm auth = new ApiAuthAlgorithm();
    private static final String signature = auth.getSignature(appId, secret, timestamp);

    public static String create(String query) throws IOException, InterruptedException {

        System.out.println(signature);

        // 建立链接
        ApiClient client = new ApiClient("https://zwapi.xfyun.cn");

        // 查询PPT模板信息
        String templateResult = client.getTemplateList(appId, ts, signature);
        System.out.println(templateResult);
        // 发送生成PPT请求
        String resp = client.createPPT(appId, ts, signature,query);
        System.out.println(resp);
        CreateResponse response = JSON.parseObject(resp, CreateResponse.class);
        // 利用sid查询PPT生成进度
        int progress = 0;
        ProgressResponse progressResponse;
        String url="";
        while (progress < 100) {
            String progressResult = client.checkProgress(appId, ts, signature, response.getData().getSid());
            progressResponse = JSON.parseObject(progressResult, ProgressResponse.class);
            progress = progressResponse.getData().getProcess();
            System.out.println(progressResult);
            if (progress < 100) {
                Thread.sleep(5000); // 暂停2秒
            }
            if (progress==100){
                url=progressResponse.getData().getPptUrl();
            }
        }
        return url;
    }
    public static String getTemplate() throws IOException {
        // 建立链接
        ApiClient client = new ApiClient("https://zwapi.xfyun.cn");
        String templateResult = client.getTemplateList(appId, ts, signature);
        System.out.println(templateResult);
        return templateResult;
    }
}
